//
//  ChoosePubViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/15.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "ChoosePubViewController.h"
#import "PubModel.h"
#import "PubTableViewCell.h"
#import "WebViewController.h"

@interface ChoosePubViewController ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *pubArr;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation ChoosePubViewController

// 视图的出现将tabbar隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
    
    NSString *str = [NSString stringWithFormat:@"%@的酒店",self.strName];
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    self.titleLabel.text = str;
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = self.titleLabel;
    
    self.pubArr = [NSMutableArray array];
    [self createData];
    [self createView];
    
    // 左上角返回图标
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];
}

// 返回
- (void)leftAction:(UIBarButtonItem *)barButton {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 酒店的数据解析
- (void)createData {
    self.page++;
    NSString *strUrl = [NSString stringWithFormat:@"http://open.qyer.com/qyer/hotel/search_list?checkin=2015-10-8%2000%3A00%%3A01&checkout=2015-10-9%2023%3A59%%3A59&city_id=%@&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&from_key=app_ios_index_top&lat=38.88262415509164&lon=121.5397591455269&oauth_token=3ccf6be2c77dee4d33e6d6a5eb878fc7&orderby=1&page=%ld&track_app_channel=App%%2520Store&track_app_version=6.6&track_device_info=iPhone7%%2C2&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_os=ios%%25208.1.2&track_user_id=6654242&v=1%%20",self.strId,self.page];
    
    [ZSMAFNetWork geturl:strUrl body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        
        NSMutableDictionary *strDic = result;
        NSMutableDictionary *secDic = strDic[@"data"];
        NSMutableArray *strArr = secDic[@"hotel"];
        
        for (NSMutableDictionary *dic in strArr) {
            PubModel *pubModel = [[PubModel alloc] init];
            [pubModel setValuesForKeysWithDictionary:dic];
            [self.pubArr addObject:pubModel];
        }
        [self.tableView reloadData];
        self.hud.hidden = YES;
        [self.tableView.footer endRefreshing];
        
    } failure:^(NSError *error) {
        NSLog(@"错误是:%@",error);
        
    }];
    
}

#pragma mark tableView的创建
- (void)createView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375,HEIGHT * 666)style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 150 * HEIGHT;
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
        
    }];
    [self createMJRefresh];
}

- (void)createMJRefresh {
    // 上拉加载
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(createData)];
}

// 空白页占位图
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, self.view.bounds.size.height/2, WIDTH*375, 30)];
    label.text = @"! 网速不给力";
    label.textAlignment = NSTextAlignmentCenter ;
    label.textColor = [UIColor redColor];
    return label;
}


#pragma mark tableView的数据处理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.pubArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    PubTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[PubTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:reuse];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    PubModel *pubModel = self.pubArr[indexPath.row];
    [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:pubModel.photo] placeholderImage:[UIImage imageNamed:@"占位图"]];
    cell.upLeftLabel.text = pubModel.cnname;
    cell.mistLabel.text = pubModel.enname;
    NSString *strRank = [NSString stringWithFormat:@"%@分",pubModel.ranking];
    cell.upRightLabel.text = strRank;
    cell.downLeftLabel.text = pubModel.star;
    NSString *strPrice = [NSString stringWithFormat:@"%@元",pubModel.price];
    cell.downRightLabel.text = strPrice;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WebViewController *webVC = [[WebViewController alloc] init];
    [self.navigationController pushViewController:webVC animated:YES];
    PubModel *pubModel = self.pubArr[indexPath.row];
    webVC.url = pubModel.link;
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.tableView.backgroundColor = [UIColor blackColor];
        self.titleLabel.textColor = [UIColor blackColor];
    }
    else {
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor whiteColor];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
